import Replacer, FileCreation

def generate_class(src_dir, class_path_name, gen_cpp):
	
	#src_dir = '../src/'

	# generate the namespace header, footer and the pre-processor header
	tokens = class_path_name.split( '/' )

	mod_name			= ""
	cls_name			= ""
	namespace_header	= 'namespace ming\n{\n'
	namespace_footer	= '}\n'
	module_pp			= 'ming'

	for i in range( len(tokens)-1 ):
		mod_name = mod_name + tokens[i] + '/'
		namespace_header = namespace_header + 'namespace ' + tokens[i] + '\n{\n'
		namespace_footer = namespace_footer + '}\n'
		module_pp = module_pp + '_' + tokens[i]

	cls_name = tokens[len(tokens)-1]
	
	# create the mapping for replacer
	mapping_dict = { r"{CLASS_NAME}" : lambda : cls_name,
					 r"{MODULE_NAME}" : lambda : mod_name,
					 r"{MODULE_PP}" : lambda : module_pp,
					 r"{NAMESPACE_HEADER}" : lambda : namespace_header,
					 r"{NAMESPACE_FOOTER}" : lambda : namespace_footer }

        # create dir
        FileCreation.create_path(src_dir + '/' + mod_name)
        
	# generate the cpp file
	if(gen_cpp == True):
			in_file = open("class_cpp.template")
			out_file = open( src_dir + '/' + mod_name + cls_name + '.cpp', "w")

			Replacer.replace(in_file, mapping_dict, out_file)

			out_file.close()
			in_file.close()

	# generate the header file
	in_file = open("class_h.template")
	out_file = open( src_dir + '/' + mod_name + cls_name + '.h', "w")

	Replacer.replace(in_file, mapping_dict, out_file)

	out_file.close()
	in_file.close()

#end def
